home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Online / FlushHeaders / new-source / NewSource.lha / get_folder_path.c < prev    next >
C/C++ Source or Header  |  2001-05-31  |  9KB  |  381 lines

  1. /*
  2. **   YAM2NN - Usenet access for YAM p7 and newer
  3. **   Copyright (C) 1999 Karol Bryd <kbryd@femina.com.pl>
  4. **
  5. **   This program is free software; you can redistribute it and/or
  6. **   modify it under the terms of the GNU General Public License
  7. **   as published by the Free Software Foundation; either version 2
  8. **   of the License, or (at your option) any later version.
  9. **
  10. **   This program is distributed in the hope that it will be useful,
  11. **   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. **   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. **   GNU General Public License for more details.
  14. **
  15. **   You should have received a copy of the GNU General Public License
  16. **   along with this program; if not, write to the Free Software
  17. **   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19.  
  20. /*
  21. ** $Log$
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28.  
  29. #include <proto/dos.h>
  30. #include <proto/exec.h>
  31. #include <proto/utility.h>
  32. #include <proto/rexxsyslib.h>
  33.  
  34. #include <rexx/rxslib.h>
  35. #include <rexx/rexxio.h>
  36. #include <rexx/errors.h>
  37.  
  38. #include <dos/dos.h>
  39. #include <exec/execbase.h>
  40. #include <exec/memory.h>
  41. #include <utility/tagitem.h>
  42.  
  43. #include "get_folder_path.h"
  44.  
  45. extern char *Stptok(const char *s, char *tok, int toklen, const char *brk);
  46. extern char *Stpblk(const char *str);
  47. extern int Astcsma(char *s, char *p);
  48.  
  49. #define stpblk Stpblk
  50. #define stptok Stptok
  51. #define astcsma Astcsma
  52.  
  53. __stkargs void NewList(struct List * list);
  54.  
  55. #ifndef YOH
  56. extern char * stristr(char * buf, char * str);
  57. #endif
  58. char current_user_path[108];
  59. char tmp[1024];
  60.  
  61. struct NewsNode {
  62.     struct NewsNode * next;
  63.     struct NewsNode * prev;
  64.     char * ngname;
  65.     char * path;
  66.     char folder[40];
  67. };
  68.  
  69. // gets path for current user's directory
  70.  
  71. // this needs to be rewritten
  72. int query_for_user(void)
  73. {
  74.     BPTR fh;
  75.     char * ln;
  76.  
  77.     SPrintf(tmp, "/* */;OPTIONS RESULTS;ADDRESS YAM;USERINFO STEM u.;call open(f,'T:uitmp','w');call writeln(f,u.maildir);call close(f)");
  78.     if(!(fh = Open("T:qfu.rexx", MODE_NEWFILE)))
  79.         return(-1);
  80.     Write(fh, tmp, strlen(tmp));
  81.     Close(fh);
  82.     Execute("rx T:qfu.rexx", NULL, NULL);
  83.     if(!(fh = Open("T:uitmp", MODE_OLDFILE)))
  84.         return(-1);
  85.     Read(fh, current_user_path, sizeof(current_user_path));
  86.     Close(fh);
  87.     DeleteFile("T:qfu.rexx");
  88.     DeleteFile("T:uitmp");
  89.     if(ln = strchr(current_user_path, 10))
  90.         *ln = 0;
  91.     return(0);
  92. }
  93.  
  94. #ifdef YOH
  95.  
  96. char * stristr(char * buf, char * str)
  97. {
  98.     register int len = strlen(buf);
  99.     register int len2 = strlen(str);
  100.     register int a;
  101.  
  102.     for(a = 0; a <= len - len2; a++)
  103.         if(Strnicmp(&buf[a], str, len2) == 0)
  104.             return(&buf[a]);
  105.     return(0);
  106. }
  107.  
  108. #endif
  109.  
  110. // scans YAM's folders and gets type of folder from .fconfig files
  111.  
  112. void scan_folders(struct List * folders)
  113. {
  114.     struct Folder * folder;
  115.     BPTR fh;
  116.  
  117.     folder = (struct Folder *)folders->lh_Head;
  118.     while(folder->f_next)
  119.     {
  120.         strcpy(tmp, current_user_path);
  121.         AddPart(tmp, folder->f_path, sizeof(tmp));
  122.         AddPart(tmp, ".fconfig", sizeof(tmp));
  123.         if(fh = Open(tmp, MODE_OLDFILE))
  124.         {
  125.             char * fconfig, * equal;
  126.  
  127.             if(fconfig = AllocVec(1000, MEMF_ANY))
  128.             {
  129.                 Read(fh, fconfig, 1000);
  130.                 equal = (char *)stristr(fconfig, "Type");
  131.                 if(equal = strchr(equal, '='))
  132.                 {
  133.                     equal++;
  134.                     equal = stpblk(equal);
  135.                     folder->f_type = atol(equal);
  136.                 }
  137.                 FreeVec(fconfig);
  138.             }
  139.             Close(fh);
  140.         }
  141.         folder = folder->f_next;
  142.     }
  143. }
  144.  
  145. // creates list of folders - only in YAM p7+
  146.  
  147. int build_folders_list(struct List * folders)
  148. {
  149.     BPTR folders_fh;
  150.     int num = 0;
  151.  
  152.     strcpy(tmp, current_user_path);
  153.     AddPart(tmp, ".folders", sizeof(tmp));
  154.     if(!(folders_fh = Open(tmp, MODE_OLDFILE)))
  155.         return(-2);  // perhaps no YAM P7
  156.     FGets(folders_fh, tmp, sizeof(tmp));
  157.     while(FGets(folders_fh, tmp, sizeof(tmp)))
  158.     {
  159.         if(Strnicmp(tmp, "@FOLDER", 7) == 0)
  160.         {
  161.             struct Folder * folder;
  162.             char tmpbuf[256];
  163.  
  164.             FGets(folders_fh, tmp, sizeof(tmp));
  165.             if(folder = AllocVec(sizeof(struct Folder), MEMF_ANY | MEMF_CLEAR))
  166.             {
  167.                 folder->f_num = num++;
  168.                 if(!(stristr(tmp, ":")))
  169.                 {
  170.                     strcpy(tmpbuf, current_user_path);
  171.                     AddPart(tmpbuf, tmp, sizeof(tmpbuf));
  172.                 }
  173.                 else
  174.                     strcpy(tmpbuf, tmp);
  175.                 if(folder->f_path = AllocVec(strlen(tmpbuf) + 1, MEMF_ANY | MEMF_CLEAR))
  176.                     strcpy(folder->f_path, strtok(tmpbuf, "\n"));
  177.                 else
  178.                     return(-2);
  179.                 AddTail(folders, (struct Node *)folder);
  180.             }
  181.             else
  182.                 return(-2);
  183.         }
  184.         if(Strnicmp(tmp, "@SEPARATOR", 10) == 0)
  185.             num++;
  186.     }
  187.     scan_folders(folders);
  188.     Close(folders_fh);
  189.     return(0);
  190. }
  191.  
  192. void free_folders_list(struct List * folders)
  193. {
  194.     struct Folder * folder = (struct Folder *)folders->lh_Head;
  195.     struct Folder * next;
  196.  
  197.     while(folder->f_next)
  198.     {
  199.         next = folder->f_next;
  200.         FreeVec(folder->f_path);
  201.         FreeVec(folder);
  202.         folder = next;
  203.     }
  204.     FreeVec(folders);
  205. }
  206.  
  207. // gets folder path
  208.  
  209. char * get_folder_path(struct List * folders, int type)
  210. {
  211.     struct Folder * folder = (struct Folder *)folders->lh_Head;
  212.  
  213.     while(folder->f_next)
  214.     {
  215.         if(folder->f_type == type)
  216.             return(folder->f_path);
  217.         folder = folder->f_next;
  218.     }
  219.     return(NULL);
  220. }
  221.  
  222. // gets folder number
  223.  
  224. int get_folder_pos(struct List * folders, int type)
  225. {
  226.     struct Folder * folder = (struct Folder *)folders->lh_Head;
  227.  
  228.     while(folder->f_next)
  229.     {
  230.         if(folder->f_type == type)
  231.             return(folder->f_num);
  232.         folder = folder->f_next;
  233.     }
  234.     return(-1);
  235. }
  236.  
  237. struct List * init_folder_list(void)
  238. {
  239.     static struct List * folders;
  240.  
  241.     if(!(folders = AllocVec(sizeof(struct List), MEMF_ANY | MEMF_CLEAR)))
  242.         return(NULL);
  243.     NewList(folders);
  244. //    folders->lh_Head = (struct Node *)&folders->lh_Tail;
  245. //    folders->lh_Tail = 0L;
  246. //    folders->lh_TailPred = (struct Node *)&folders->lh_Head;
  247. //    folders->lh_Type = 0;
  248.     return(folders);
  249. }
  250.  
  251. void save_folder_list(struct List * folders, struct List * newsgroups)
  252. {
  253.     struct Folder * folder;
  254.     struct NewsNode * nc;
  255.     BPTR fh;
  256.  
  257.     if(!folders)
  258.         return;
  259.     if(!(fh = Open("YAM:YAM2NN.folders", MODE_NEWFILE)))
  260.         return;
  261.     Write(fh, "Folderfiledate: -1\n", 19);
  262.     folder = (struct Folder *)folders->lh_Head;
  263.     while(folder->f_next)
  264.     {
  265.         if(newsgroups)
  266.         {
  267.             if((folder->f_type == INCOMING) || (folder->f_type == OUTGOING) || (folder->f_type == SENT) || (folder->f_type == DELETED))
  268.             {
  269.                 SPrintf(tmp, "!%s!%ld!%ld!\n", folder->f_path, folder->f_num, folder->f_type);
  270.                 Write(fh, tmp, strlen(tmp));
  271.             }
  272.             else
  273.             {
  274.                 for(nc = (struct NewsNode *)newsgroups->lh_Head; nc->next; nc = (struct NewsNode *)nc->next)
  275.                 {
  276.                     if(stristr(nc->path, FilePart(folder->f_path)))
  277.                     {
  278.                         SPrintf(tmp, "!%s!%ld!%ld!\n", folder->f_path, folder->f_num, folder->f_type);
  279.                         Write(fh, tmp, strlen(tmp));
  280.                         break;
  281.                     }
  282.                 }
  283.             }
  284.         }
  285.         else
  286.         {
  287.             SPrintf(tmp, "!%s!%ld!%ld!\n", folder->f_path, folder->f_num, folder->f_type);
  288.             Write(fh, tmp, strlen(tmp));
  289.         }
  290.         folder = folder->f_next;
  291.     }
  292.     Close(fh);
  293. }
  294.  
  295. struct List * open_folder_list(void)
  296. {
  297.     struct Folder * folder;
  298.     struct List * list;
  299.     char name[108];
  300.     BPTR fh;
  301.  
  302.     if(!(list = AllocVec(sizeof(struct List), MEMF_ANY | MEMF_CLEAR)))
  303.         return(NULL);
  304.     NewList(list);
  305. //    list->lh_Head = (struct Node *)&list->lh_Tail;
  306. //    list->lh_Tail = 0L;
  307. //    list->lh_TailPred = (struct Node *)&list->lh_Head;
  308. //    list->lh_Type = 0;
  309.     if(!(fh = Open("YAM:YAM2NN.folders", MODE_OLDFILE)))
  310.     {
  311.         FreeVec(list);
  312.         return(NULL);
  313.     }
  314.     while(FGets(fh, tmp, sizeof(tmp)))
  315.     {
  316.         char * start, * end;
  317.  
  318.         if(!(folder = AllocVec(sizeof(struct Folder), MEMF_ANY | MEMF_CLEAR)))
  319.             break;
  320.         if(end = strrchr(tmp, '!'))
  321.             *end = 0;
  322.         start = strrchr(tmp, '!');
  323.         folder->f_type = atol(++start);
  324.         *start = 0;
  325.         if(end = strrchr(tmp, '!'))
  326.             *end = 0;
  327.         start = strrchr(tmp, '!');
  328.         folder->f_num = atol(++start);
  329.         *start = 0;
  330.         if(end = strrchr(tmp, '!'))
  331.             *end = 0;
  332.         start = strrchr(tmp, '!');
  333.         strcpy(name, ++start);
  334.         if(folder->f_path = AllocVec(strlen(name) + 1, MEMF_ANY | MEMF_CLEAR))
  335.             strcpy(folder->f_path, name);
  336.         else
  337.             return(NULL);
  338.         AddTail(list, (struct Node *)folder);
  339.     }
  340.     Close(fh);
  341.     return(list);
  342. }
  343.  
  344. #ifdef YOH
  345.  
  346. void _main(void)
  347. {
  348.     struct List * folders;
  349.     struct Folder * folder;
  350.  
  351.     if(SysBase->LibNode.lib_Version < 39)
  352.         _exit(20);
  353.     query_for_user();
  354.     Printf("current user path: \'%s\'\n", current_user_path);
  355.     if(!(folders = open_folder_list()))
  356.     {
  357.         folders = init_folder_list();
  358.         if(build_folders_list(folders) != -2)
  359.         {
  360.             save_folder_list(folders, NULL);
  361.             free_folders_list(folders);
  362.             folders = open_folder_list();
  363.             Printf("INCOMING: \'%s\'\n", get_folder_path(folders, INCOMING));
  364.             Printf("DELETED at %ld position\n", get_folder_pos(folders, DELETED));
  365.         }
  366.         else
  367.             _exit(-2);
  368.     }
  369.     folder = (struct Folder *)folders->lh_Head;
  370.     folder = folder->f_next;
  371.     Printf("Folderpath, foldernumber and foldertype ...\n");
  372.     while(folder->f_next)
  373.     {
  374.         Printf("\'%s\' %ld %ld\n", folder->f_path, folder->f_num, folder->f_type);
  375.         folder = folder->f_next;
  376.     }
  377.     free_folders_list(folders);
  378. }
  379.  
  380. #endif
  381.